package by.gusto.afisha;

import java.io.File;

import org.acra.ACRA;
import org.acra.annotation.ReportsCrashes;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;

import by.gusto.afisha.db.HelperFactory;

import android.app.Application;
import android.graphics.Bitmap.CompressFormat;

@ReportsCrashes(formKey = "dF9ONlo4cFZEU3ZGa1A2dXBVbFpFaXc6MQ")
public class MyApplication extends Application {
 
	 @Override
	  public void onCreate() {
	      super.onCreate();
	      HelperFactory.SetHelper(getApplicationContext());

	      // The following line triggers the initialization of ACRA
	      ACRA.init(this);
	      
	      // init universal image loader
	      initImageLoader();
	  }
	 
	private void initImageLoader() {
		File cacheDir = StorageUtils.getCacheDirectory(this);
		
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
	        .memoryCacheExtraOptions(480, 800) // width, height
	        .discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75) // width, height, compress format, quality
	        .threadPoolSize(5)
	        .threadPriority(Thread.MIN_PRIORITY + 2)
	        .denyCacheImageMultipleSizesInMemory()
	        .offOutOfMemoryHandling()
	        .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // 2 Mb
	        .discCache(new UnlimitedDiscCache(cacheDir))
	        .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
	        .imageDownloader(new BaseImageDownloader(getApplicationContext(), 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)
	        .defaultDisplayImageOptions(DisplayImageOptions.createSimple())
	        .enableLogging()
	        .build();
		
		ImageLoader.getInstance().init(config);
	}

	@Override
	public void onTerminate() {
		HelperFactory.ReleaseHelper();
		super.onTerminate();
	}
}
